﻿using MES.Server.Common;
using MES.Server.Entities;
using MES.Server.IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MES.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MenuController : ControllerBase
    {
        IMenuService _menuService;
        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }


        // http://localhost:5037/api/menu/all?key=ewfwe
        // http://localhost:5037/api/menu/all?key=
        // http://localhost:5037/api/menu/all/ewfwe
        // http://localhost:5037/api/menu/all/none
        // http://localhost:5037/api/menu/all     (通过POST传  Body  Form)
        [HttpGet("all/{key}")]
        //[Authorize]
        public async Task<JsonResult> GetAllMenus([FromRoute] string key)
        {
            ApiDataResult<MenuEntity[]> result = new ApiDataResult<MenuEntity[]>();
            try
            {
                key = key == "none" ? "" : key;
                var ms = _menuService.GetAllMenus(key);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("menus/{uid}")]
        [Authorize]
        public async Task<JsonResult> GetMenuByUser(int uid)
        {
            ApiDataResult<MenuEntity[]> result = new ApiDataResult<MenuEntity[]>();
            try
            {
                var ms = _menuService.GetMenusByUser(uid);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("menus/role")]
        [Authorize]
        public async Task<JsonResult> GetMenuByRoles(int[] rid)
        {
            ApiDataResult<MenuEntity[]> result = new ApiDataResult<MenuEntity[]>();
            try
            {
                var ms = _menuService.GetMenusByRoles(rid);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("update")]
        [Authorize]
        public async Task<JsonResult> Update(MenuEntity menu)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _menuService.Update(menu);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("delete/{id}")]
        [Authorize]
        public async Task<JsonResult> DeleteMenu([FromRoute] string id)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _menuService.Delete(id);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("delete/ids")]
        [Authorize]
        public async Task<JsonResult> DeleteMenu(string[] ids)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _menuService.Delete(ids);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }
    }
}
